/*
 * SMSServer.java
 *
 * Created on May 15, 2008, 2:13 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;

public class SMSServer implements MessageListener, Runnable {

    private static Command displayCmd = new Command("Play", Command.OK, 1);
    private static Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
    private static final int PLAYING = 0;
    private static final int OVER = 1;
    private int ACTION = 1;
    private MessageConnection servConn;
    private static final String PROTOCOL = "sms://";
    private Vector temp = new Vector();
    private Displayable tmpDisplayable;
    private MorseME midlet;

    public SMSServer(int default_port, MorseME midlet) {
        
        this.midlet = midlet;
        
        try {
            servConn = (MessageConnection) Connector.open(PROTOCOL + ":" + MorseME.DEFAULT_PORT);
            servConn.setMessageListener(this);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void close() throws IOException {

        if (servConn != null) {
            servConn.close();
        }
    }

    private void setState(int status) {
        this.ACTION = status;
    }

    private int getState() {
        return this.ACTION;
    }

    public void notifyIncomingMessage(MessageConnection conn) {
        
        if (conn == servConn) {
            Thread t = new Thread(this);
            t.start();
        }
    }

    private void playSMS(int loop) {

        setState(PLAYING);

        String recvSMS = ((TextMessage) temp.elementAt(0)).getPayloadText();

        if (recvSMS != null) {

            for (int i = 0; i < loop; i++) {

                try {
                    if (i > 0) {
                        Thread.sleep(20000);
                    }
                    if(recvSMS.indexOf("none#none#") != -1){
                        String leftString = null;
                        leftString = recvSMS.substring(recvSMS.indexOf("#")+1);
                        recvSMS = leftString.substring(leftString.indexOf("#")+1);
                        midlet.play(MorseProcess.translate(recvSMS));
                         
                    }
                    
                    

                } catch (InterruptedException ex) {

                    ex.printStackTrace();
                }
            }
        }

        setState(OVER);

        synchronized (this) {

            temp.removeElementAt(0);
            temp.trimToSize();
        }

        if (temp.size() > 0) {
            playSMS(loop);
        }

        Inbox inbox = Inbox.getInbox();
        inbox.updateInbox();
        Display.getDisplay(MorseME.getMIDlet()).setCurrent(inbox);
    }

    public void run() {

        try {
            Inbox inbox = Inbox.getInbox();
            if (inbox == null) {
                return;
            }

            synchronized (this) {

                Message recvMsg = this.servConn.receive();
                inbox.addMessage(recvMsg);
                temp.addElement(recvMsg);
            }

            if (getState() == PLAYING) {

                return;
            }

            Displayable current = Display.getDisplay(MorseME.getMIDlet()).getCurrent();

            if (current.isShown()) {

                if (current == tmpDisplayable) {
                    inbox.updateInbox();
                    return;
                }

                Alert alert = new Alert("You got the message!!", "Do you want to play the message ?", null, AlertType.ALARM);

                tmpDisplayable = alert;

                if (Option.AUTO_SEL == 1) {
                    alert.addCommand(displayCmd);
                    alert.addCommand(cancelCmd);
                    alert.setCommandListener(new SMSAlertListener());
                    setState(PLAYING);
                } else if (Option.AUTO_SEL == 0) {
                    alert.setTimeout(3000);
                    setState(PLAYING);
                    playSMS(Option.NUM_AUTO_PLAY);
                }

                Display.getDisplay(MorseME.getMIDlet()).setCurrent(alert);
            }

        } catch (IOException ex) {

            setState(OVER);
            ex.printStackTrace();
        }
    }

    private class SMSAlertListener implements CommandListener {

        public void commandAction(Command c, Displayable d) {

            if (c == displayCmd) {

                playSMS(Option.NUM_AUTO_PLAY);

            } else if (c == cancelCmd) {

                setState(OVER);
                midlet.stop();

                Inbox inbox = Inbox.getInbox();
                inbox.updateInbox();
                Display.getDisplay(MorseME.getMIDlet()).setCurrent(inbox);

            }
        }
    }
}

